package cn.kgc.vue.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.LogInfo;
import cn.kgc.vue.service.LogInfoService;
import cn.kgc.vue.mapper.LogInfoMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* @author mengshun
* @description 针对表【sys_log_info】的数据库操作Service实现
* @createDate 2024-04-17 10:31:01
*/
@Service
public class LogInfoServiceImpl extends ServiceImpl<LogInfoMapper, LogInfo> implements LogInfoService{


    @Override
    public ResponseResult logsPage(QueryCondition<LogInfo> queryCondition) {

        // 创建分页对象
        Page<LogInfo> logInfoPage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());

        // 获取搜索参数
        LogInfo searchParams = queryCondition.getSearchParams();
        String username = searchParams.getUsername();
        String model = searchParams.getModel();
        String ip = searchParams.getIp();

        LambdaQueryWrapper<LogInfo> lambda = new QueryWrapper<LogInfo>().lambda();
        lambda.eq(StringUtils.isNotBlank(username),LogInfo::getUsername,username)
                .eq(StringUtils.isNotBlank(model),LogInfo::getModel,model)
                .eq(StringUtils.isNotBlank(ip),LogInfo::getIp,ip)
                .orderByDesc(LogInfo::getCreateTime);

        baseMapper.selectPage(logInfoPage,lambda);


        return ResponseResult.success()
                .data("total",logInfoPage.getTotal())
                .data("tableData",logInfoPage.getRecords());
    }

    @Override
    public void exportLogs(HttpServletResponse response, Map<String, String> params) {

        String username = params.get("username");
        String ip = params.get("ip");
        String model = params.get("model");

        LambdaQueryWrapper<LogInfo> lambda = new QueryWrapper<LogInfo>().lambda();
        lambda.eq(StringUtils.isNotBlank(username),LogInfo::getUsername,username)
                .eq(StringUtils.isNotBlank(model),LogInfo::getModel,model)
                .eq(StringUtils.isNotBlank(ip),LogInfo::getIp,ip)
                .orderByDesc(LogInfo::getCreateTime);

        List<LogInfo> logInfos = baseMapper.selectList(lambda);

        // 导出数据
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName("日志信息表");
        exportParams.setTitle("用户操作日志信息");

        Workbook workBook = ExcelExportUtil.exportExcel(exportParams, LogInfo.class, logInfos);

        try {
            ServletOutputStream outputStream = response.getOutputStream();
            workBook.write(outputStream);
            outputStream.close();
            workBook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}




